package com.api.manage.redis;

import com.api.manage.feign.product.ProductClientService;
import com.api.manage.feign.system.SystemClientService;
import com.api.manage.pojo.product.SysCategoryBean;
import com.api.manage.pojo.system.SysBranchOfficeResources;
import com.api.manage.pojo.system.SystemLevelBranchResources;
import com.tem.common.jdbc.utils.RedisUtils;
import com.tem.common.tool.result.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class RedisCacheUtil {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SystemClientService systemClientService;
    @Autowired
    private ProductClientService productClientService;
    public static final String NameCache="NameCache",//hash类型
            SysCategoryName="SysCategoryName",//系统栏目
            SysCompanyName="SysCompanyName",//企业
            SysBranchOfficeName="SysBranchOfficeName",//分公司
            SysCategoryPathName="SysCategoryPathName",//栏目路径名称
            SysBranchOfficeResources="SysBranchOfficeResources",//分公司、分店资源缓存
            SysRoleDataScopeAll="SysRoleDataScopeAll"//用户-角色-权限
                    ;
    public String getSysCategoryName(Long id){
        String cname = redisUtils.getName(SysCategoryName,id.toString());
        if(cname!=null)return cname;
        ResultUtil<SysCategoryBean> scb = productClientService.getSysCategoryBeanById(id);
        if(scb!=null&&scb.isSuccess()&&scb.getData()!=null){
            redisUtils.setName(SysCategoryName,id.toString(),scb.getData().getCategoryName());
            cname=scb.getData().getCategoryName();
        }
        return cname;
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    public String getSysCategoryPathName(Long id){
        String cname = redisUtils.getName(SysCategoryPathName,id.toString());
        if(cname!=null)return cname;
        ResultUtil<SysCategoryBean> scb = productClientService.getSysCategoryBeanById(id);
        if(scb!=null&&scb.isSuccess()&&scb.getData()!=null && scb.getData().getCategoryPathName()!=null){
            redisUtils.setName(SysCategoryPathName,id.toString(),scb.getData().getCategoryPathName());
            cname=scb.getData().getCategoryPathName();
        }
        return cname;
    }

    /**
     * 根据路径获取
     * @param path
     * @return
     */
    public String getSysCategoryPathName(String path){
        String[] ids = path.split("\\.");
        if(ids!=null && ids.length>0){
            int idindex = ids.length;
            String id = ids[idindex-1];
            if(id == null || id.trim().length()<1) id = ids[idindex-2];
            return getSysCategoryPathName(new Long(id));
        }
        return null;
    }
    /**
     * 通用获取值，不加缓存
     * @param key
     * @param id
     * @return
     */
    public String getName(String key,Long id){
        return redisUtils.getName(key,id.toString());
    }
    public Object getObject(String key,Long id){
        return redisUtils.getObject(key,id.toString());
    }
    /**
     * 加入缓存，通用接口
     * @param key
     * @param id
     * @param value
     */
    public void setName(String key,Long id,String value){
        redisUtils.setName(key,id.toString(),value);
    }
    public void setObject(String key,Long id,Object value){
        redisUtils.setObject(key,id.toString(),value);
    }

    @ApiOperation(value = "获取分公司 已购买的所有系统资源")
    public SysBranchOfficeResources getSysBranchOfficeResources(Long sysBranchOfficeId){
        SysBranchOfficeResources resources = (SysBranchOfficeResources)getObject(RedisCacheUtil.SysBranchOfficeResources , sysBranchOfficeId);//从缓存获取
        if(resources!=null) return resources;
        ResultUtil<SysBranchOfficeResources> sbor = systemClientService.getAllResources(sysBranchOfficeId);//从mysql获取
        if(sbor.isSuccess()){
            setObject(RedisCacheUtil.SysBranchOfficeResources,sysBranchOfficeId,resources);
            resources = sbor.getData();
        }
        return resources;
    }
    @ApiOperation(value = "获取系统名称")
    public String getSystemTypeName(Long sysBranchOfficeId,Long systemLevelId){
        if(sysBranchOfficeId==null || systemLevelId==null)return null;
        SysBranchOfficeResources resources = this.getSysBranchOfficeResources(sysBranchOfficeId);
        if(resources==null || resources.getSystemLevelBranchs()==null || resources.getSystemLevelBranchs().size()<1)return "无效数据";
        for(SystemLevelBranchResources r:resources.getSystemLevelBranchs()){
            if(systemLevelId.equals(r.getSystemLevelId()))return r.getTypeName();
        }
        return null;
    }
    @ApiOperation(value = "获取系统资源对象ID")
    public Long getSystemLevelId(Long sysBranchOfficeId,String sysCode){
        if(sysBranchOfficeId==null || sysCode==null)return null;
        SysBranchOfficeResources resources = this.getSysBranchOfficeResources(sysBranchOfficeId);
        if(resources==null || resources.getSystemLevelBranchs()==null || resources.getSystemLevelBranchs().size()<1)return null;
        for(SystemLevelBranchResources r:resources.getSystemLevelBranchs()){
            if(sysCode.equals(r.getSysCode()))return r.getSystemLevelId();
        }
        return null;
    }
}
